<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <link rel="stylesheet" href="../css/style.css">
    <script src="../plugins/echarts/echarts.js"></script>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>统计分析<small>会员数量</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>统计分析</el-breadcrumb-item>
            <el-breadcrumb-item>会员数量</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <!-- 为 ECharts 准备一个具备大小（宽高）的 DOM -->
            <div id="chart1" style="height:600px;"></div>
            <div class="block" style="text-align: center">
                <span class="demonstration">选择查看日期范围</span>
                <el-date-picker unlink-panels
                                v-model="value_date"
                                type="monthrange"
                                align="right"
                                unlink-panels
                                range-separator="至"
                                start-placeholder="开始月份"
                                end-placeholder="结束月份"
                                :picker-options="pickerOptions">
                </el-date-picker>
                <el-button value="更改日期" @click="getDate()">更改日期</el-button>
            </div>
        </div>
    </div>
</div>

</div>

</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script type="text/javascript">

    /*日历控件*/
    /*
    3、会员数量报表指定时间段展示
         说明：目前我们项目中的会员数量折线图是固定展示的过去一年的会员数量，现在需要改为在页面中通
         过两个日历控件来选择时间段，根据所选的时间段来动态展示此时间段内的每个月会员数量
    */
    // axios跨域访问，如果需要携带cookie，withCredentials必须为true
    axios.defaults.withCredentials = true;
    var vue = new Vue({
        el: '#app',
        data: {
            backend_url: "http://api.oms.health.itcast.com:9002",
            pickerOptilons: {
                shortcuts: [{
                    text: '本月',
                    onClick(picker) {
                        picker.$emit('pick', [new Date(), new Date()]);
                    }
                }, {
                    text: '今年至今',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date(new Date().getFullYear(), 0);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近六个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setMonth(start.getMonth() - 6);
                        picker.$emit('pick', [start, end]);
                    }
                }]
            },
            value_date: ''
        },
        methods: {
            getDate() {
                axios.post(this.backend_url + "/report/getMemberReport2.do", this.value_date).then((response) => {
                    if (response.data.flag) {
                        //更新模型中的信息
                        memberOption.xAxis.data = response.data.data.months;
                        memberOption.series[0].data = response.data.data.memberCount;
                        myChart1.clear();
                        myChart1.setOption(memberOption);
                    }
                }).catch((error) => {
                    console.log(error);
                    this.$message.error("网络异常");
                });
            }
        }
    });


    // 基于准备好的dom，初始化echarts实例
    var myChart1 = echarts.init(document.getElementById('chart1'));

    // 使用刚指定的配置项和数据显示图表。

    var memberOption = {
        title: {
            text: '会员数量'
        },
        tooltip: {},
        legend: {
            data: ['会员数量']
        },
        xAxis: {
            data: []
        },
        yAxis: {
            type: 'value'
        },
        series: [{
            name: '会员数量',
            type: 'line',
            data: []
        }]
    };
    myChart1.setOption(memberOption);

    const end = new Date();
    const start = new Date();
    start.setMonth(start.getMonth() - 6);
    var initDate = [start, end]
    axios.post("http://api.oms.health.itcast.com:9002/report/getMemberReport2.do", initDate).then((response) => {
        if (response.data.flag) {
            //myChart1.setOption();
            memberOption.xAxis.data = response.data.data.months;
            memberOption.series[0].data = response.data.data.memberCount;
            myChart1.clear();
            myChart1.setOption(memberOption);
        }
    }).catch((error) => {
        console.log(error);
        this.$message.error("网络异常");
    });

</script>
</html>
